/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.qdu.dao.impl;

import com.qdu.dao.BookDao;
import com.qdu.entity.Book;
import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Lenovo
 */
public class BookDaoImpl extends BaseDaoImpl2 implements BookDao {

    @Override
    public int insert(Book b) {
        return executeUpdate("insert into Book values(?,?,?,?,?,?,?,?,?,?)",
                b.getbId(),
                b.getbName(),
                null,
                null,
                b.getbCover(),
                null,
                null,
                null,
                null,
                b.getBrating()
                );

    }

    @Override
    public int update(Book b) {

        return executeUpdate("update Book set BName=?,BRating=? where BID=?",
                b.getbName(), b.getBrating(), b.getbId()
        );
    }

    @Override
    public int deleteBybId(String bId) {
        return executeUpdate("delete Book where BID=?", bId);
    }

    @Override
    public List<Book> getAllBooks() {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Book> list = new ArrayList<>();

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select BID,BName,BRating,BCover from Book");
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Book(rs.getString(1), rs.getString(2), rs.getFloat(3), rs.getString(4)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }

    @Override
    //刘萌
    public List<Book> getBooksByKeyword(String bookName) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Book> list = new ArrayList<>();

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select BID,BName,BRating,Byear,Bpublisher from Book where BName like ?");
            ps.setString(1, "%" + bookName + "%");
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Book(rs.getString(1), rs.getString(2), rs.getFloat(3), rs.getString(4), rs.getString(5)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }

    @Override
    public Book getBookInfoById(String bookId) {

        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select * from Book where BID=?");
            ps.setString(1, bookId);
            rs = ps.executeQuery();
            while (rs.next()) {
                return new Book(
                        rs.getString(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getString(4),
                        rs.getString(5),
                        rs.getString(6),
                        rs.getString(7),
                        rs.getString(8),
                        rs.getString(9),
                        rs.getFloat(10)
                );
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return null;
    }
}
